from mitmproxy import http
from termcolor import colored
import json

line_length = 40


def parse_json(content):
    content = content.decode()
    json_data = {}
    for item in content.split('&'):
        data = item.split('=')
        json_data[data[0]] = data[1]
    return json_data


def request(flow: http.HTTPFlow) -> None:
    if flow.request.method == "POST":
        print(colored('-'*line_length+"Request Header " + '-'*line_length, 'red'))
        for i, j in flow.request.headers.items():
            print('"{}":"{}"'.format(i, j))
        print(colored('-' * line_length +
                      "Request Header " + '-' * line_length, 'red'))

        postData = parse_json(flow.request.content)
        print(colored('-'*line_length+"Request postData " + '-'*line_length, 'red'))
        for k, v in postData.items():
            print('"{}":"{}"'.format(k, v))
        print(colored('-'*line_length+"Request postData " + '-'*line_length, 'red'))
